﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace TimeCounterISM
{
    public class TimeCounterISMGroup
    {
        private Dictionary<string, TimeCounterISMNode> group;
        private TimeCounterISMNode activeNode;



        public void AddCounter(string index, TimeCounterISMNode node)
        {
            group.Add(index, node);
            AddAutoPush(node);

            if (!CheckActiveNodeEmpty())
                activeNode = node;
        }

        public void Run()
        {
            if (CheckActiveNodeEmpty())
                activeNode.Run();
        }

        public void SetActiveNode(string name)
        {
            if (name == null || name == "")
                activeNode = null;
            else
                activeNode = group[name];
        }

        #region "Debug Funcs"
        public void LogActiveNodeInfor()
        {
            if(CheckActiveNodeEmpty())
                Debug.Log("Active node running time is "+activeNode.RunningTime+" and total time is "+activeNode.TotalTime);
        }
        #endregion



        private bool CheckActiveNodeEmpty()
        {
            return !(activeNode == null);
        }

        private void AddAutoPush(TimeCounterISMNode node)
        {
            node.ClearNode.AddListener(() =>
            {
                SetActiveNode(node.NextNodeName);
            });
        }



        public TimeCounterISMGroup()
        {
            group = new Dictionary<string, TimeCounterISMNode>();
        }
    }
}
